<?php

namespace App\Http\Controllers\Home;

use App\Http\Requests\PasswordRequest;
use App\models\Collect;
use App\models\Goods;
use App\models\Order;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CenterController extends CommonController
{
    public function __construct()
    {
//        调用父级构造方法
        parent::__construct();
    }

    public function index()
   {
       $collects = Collect::where('user_id',auth()->id())->limit(6)->get();
       $commend_goods = Goods::where('is_commend',1)->latest()->limit(5)->get();
       $orders = Order::where('user_id',auth()->id())->limit(3)->get();
      return view('home.center_index',compact('orders','collects','commend_goods'));
   }
   public function edit($info)
   {
      return view('home.center_' . $info);
   }
   public function checkCode(Request $request)
   {
       return view('home.center_editPassword');
   }
   //验证身份数据他提交
    public function checkCodePost(Request $request)
    {
//       dd(123);
        $status = session('register.email') == $request->input('email') && session('register.code') == $request->input('code');
        if (!$status)
        {
            return back()->with('error','验证码错误');
        }
        return redirect()->route('home.center.checkCode');
    }

   public function updateInform(User $user,Request $request)
   {
//     dd(123);
//       dd($request->all());
       $data = $request->all();
       //dd(key_exists('password',$data));
       if (key_exists('password',$data))
       {

           $this->validate($request,[
               'password'=>'required|confirmed',
           ],[
               'password.required'=>'密码不能为空',
               'password.confirmed'=>'两次密码不一致',
           ]);
           $data['password'] = bcrypt($data['password']);
           $user->update($data);
           auth()->logout();
           return redirect(route('home.login'))->with('success','密码修改成功，请重新登录');
       }
       $user->update($data);
       return back()->with('success','用户信息修改成功');
   }
//   我的订单详情
   public function myOrder()
   {
       $orders = Order::where('user_id',auth()->id())->get();
      return view('home.center_myOrder',compact('orders'));
   }
//   我的全部订单
   public function allOrder()
   {
       $orders = Order::where('user_id',auth()->id())->get();
       return view('home.center_allOrder',compact('orders'));
   }

   public function searchOrder(Request $request)
   {
//       dd($request->order_num);
       $data = $request->all();
       $order_num = $request->order_num;
       $order = Order::where('order_num',$order_num)->first();
       return view('home.center_search',compact('order'));
   }
}
